<?php
/**
 * Created by PhpStorm.
 * User: 诗觞
 * Date: 2016/7/27
 * Time: 10:44
 */
namespace app\controllers;
use app\models\Admin;
use app\models\Class1;
use app\models\Class2;
use app\models\ReadContent;
use app\models\ReadOption;
use app\models\ReadQuestion;
use app\models\SpeakContent;
use app\models\User;
use app\models\WriteQuestion;
use \yii\web\Controller;
use Yii;
use app\models\UploadForm;
class AdminController extends Controller
{
    public $layout = "index";
    public $enableCsrfValidation = true;


    /**
     * @return string
     * 后台登录控制器
     */


    public function actionLogin()
    {
        $this->layout = "main-nofoot";
        $request = \Yii::$app->request;
        $managername = $request->post('managername');
        $managerpassword = md5($request->post('managerpassword'));
        if ($request->isPost) {
            $result = Admin::find()
                        ->where(['username' => $managername, 'password' => $managerpassword])
                        ->one();
            if (isset($result)) {
                $session = Yii::$app->session;
                $session->open();
                $session['username'] = $managername;
                $session['is_admin'] = true;
                date_default_timezone_set("Asia/Shanghai");
                $session['logintime'] = date("Y-m-d H:i:s");
                $session['class'] = $result->class;
                $this->redirect('?r=admin/index');
            } else {
                echo '<script> alert("账号密码错误");</script>';
            }
        }
        return $this->render('login');
    }

    /**
     * @return string
     * 后台首页渲染控制器
     */

    public function actionIndex()
    {
        $session = Yii::$app->session;
        if ($session['is_admin'] == false || !isset($session['is_admin'])) {
            $this->redirect('?r=admin/login');
        }
        $admininfo = array(
            "username" => $session['username'],
            "is_admin" => $session['is_admin'],
            "logintime" => $session['logintime'],
            "loginip" => $_SERVER['REMOTE_ADDR'],
            "serverinfo" => $_SERVER['SERVER_NAME']
        );
        return $this->render('index', array("admininfo" => $admininfo));
    }

    /**
     * 后台管理员退出登录控制器
     */

    public function actionLogout()
    {
        $session = Yii::$app->session;
        $session->removeAll();
        $this->redirect('?r=admin/index');
    }

    /**
     * @return string
     * 后台管理员查询显示控制器
     */

    public function actionAdmininfo()
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        if ($session['class'] != '2') {
            echo "<script>alert('您没有足够的权限管理管理员');
    			location.href='?r=admin/index';
    		</script>";
        }
        if ($session['class'] == 2) {
            $adminmodel = new Admin;
            $result = Admin::find()->all();
            return $this->render('admininfo', array("admininfo" => $result));
        }
    }

    /**
     * @param $id
     * 后台管理员删除控制器
     */
    public function actionAdmindel($id)
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $adminmodel = new Admin;
        $delstatus = $adminmodel->find()->where(["id" => $id])->one();
        if ($delstatus->class == 2) {
            echo "<script>alert('无法删除超级管理员');
    			location.href='?r=admin/admininfo';
    		</script>";
        } else {
            if ($delstatus->delete()) {
                echo "<script>alert('删除管理员成功');
    			location.href='?r=admin/admininfo';
    		</script>";
            } else {
                echo "<script>alert('删除管理员失败请重新尝试');
    			location.href='?r=admin/admininfo';
    		</script>";
            }
        }
    }

    /**
     * @return string
     * 后台管理员添加控制器
     */
    public function actionAdd()
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        if ($session['class'] != 2) {
            echo "<script>alert('您没有足够的权限管理管理员');
    			location.href='?r=admin/index';
    		</script>";
            die;
        }
        $request = \Yii::$app->request;
        if ($request->isPost) {
            $class = $request->post('class');
            if ($class == null) {
                $class = 1;
            }
            $username = $request->post('username');
            $password = md5($request->post('password'));
            $info = array(
                "username" => $username,
                "password" => $password,
                "class" => $class
            );
            $adminmodel = new Admin;
            $adminadd1 = $adminmodel->username = $info['username'];
            $adminadd2 = $adminmodel->password = $info['password'];
            $adminadd3 = $adminmodel->class = $info['class'];
            $adminaddststus = $adminmodel->save();
            if ($adminaddststus) {
                echo "<script>alert('添加管理员成功');
    			location.href='?r=admin/admininfo';
    		</script>";
            } else {
                echo "<script>alert('添加管理员失败，请重试！');
    			location.href='?r=admin/admininfo';
    		</script>";
            }
        } else {
            return $this->render('adminadd');
        }
    }

    /**
     * @param int $id
     * @return string
     * 后台管理员修改控制器
     */

    public function actionUpdate($id = 0)
    {
        $request = \Yii::$app->request;
        if ($id == 0 && !$request->isPost) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        if ($request->isPost) {
            $id1 = $request->post('id');
            if ($id1 != $id && $id != 0) {
                echo "<script>alert('别闹!');
    			location.href='?r=admin/admininfo';
    		</script>";
                die;
            }
            $username = $request->post('username');
            if (strlen($request->post('password')) != 32) {
                $password = md5($request->post('password'));
                $info = array(
                    "username" => $username,
                    "password" => $password,
                    "id" => $id1
                );
                $adminmodel = new Admin;
                $adminadd = $adminmodel->findOne($info['id']);
                $adminadd->username = $info['username'];
                $adminadd->password = $info['password'];
            } else {
                $info = array(
                    "username" => $username,
                    "id" => $id1
                );
                $adminmodel = new Admin;
                $adminadd = $adminmodel->findOne($info['id']);
                $adminadd->username = $info['username'];
            }
            $adminaddststus = $adminadd->update();
            if ($adminaddststus) {
                echo "<script>alert('修改管理员成功');
    			location.href='?r=admin/admininfo';
    		</script>";
            } else {
                echo "<script>alert('修改管理员失败，请重试！');
    			location.href='?r=admin/admininfo';
    		</script>";
            }
        } else {
            $adminmodel = new Admin;
            $delstatus = $adminmodel->find()->where(["id" => $id])->one();
            $admininfo = $delstatus;
            return $this->render('adminupdate', array("admininfo" => $admininfo));
        }
    }

    /**
     * @return string
     * 阅读题目查询控制器
     */

    public function actionReadinginfo()
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $readmodel1 = new ReadContent();
        $readmodel2 = new ReadQuestion();
        $readinfo1 = $readmodel1->find()->orderBy(['class_num'=>SORT_ASC])->all();
        foreach ($readinfo1 as $s) {
            $readinfo2 = $readmodel2->find()->where(['read_content_id' => $s->id])->all();
            if ($s->diff == 1) {
                $diff = "简单";
            } elseif ($s->diff == 2) {
                $diff = "适中";
            } else {
                $diff = "难";
            }
            $class1model = new Class1();
            $class1 = $class1model->find()->where(["id" => $s->class1_id])->one();
            $class1mode2 = new Class2();
            $class2 = $class1mode2->find()->where(["id" => $s->class2_id])->one();
            $readinfo[] = array(
                "id" => $s->id,
                "title" => $s->title,
                "info" => $s->info,
                "tnum" => count($readinfo2),
                "num" => $s->num,
                "diff" => $diff,
                "class1" => $class1->name,
                "class2" => $class2->name,
                "imgurl" => $s->img_url,
                "class_num" => $s->class_num
            );
        }
        if (!$readinfo1) {
            $readinfo = array();
        }
        return $this->render('readinginfo', array("readinfo" => $readinfo));
    }

    public function actionReaddel($id)
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $readmodel = new ReadContent();
        $readinfo = $readmodel->find()->where(["id" => $id])->one();
        $readquemodel = new ReadQuestion();
        $readqueinfo = $readquemodel->find()->where(["read_content_id" => $id])->all();
        if ($readqueinfo) {
            foreach ($readqueinfo as $s) {
                $s->delete();
            }
            if ($readinfo->delete()) {
                return true;
            } else {
                return false;
            }
        } else {
            if ($readinfo->delete()) {
                return true;
            } else {
                return false;
            }
        }
    }


    /**
     * @return string
     * ajax请求，用来得到class1的类型
     */
    public function actionGetreadtype1()
    {
        $class1model = new Class1();
        $class1 = $class1model->find()->all();
        $class1info = "";
        foreach ($class1 as $s) {
            $class1info .= $s->id . " " . $s->name . "/";
        }
        return $class1info;
    }


    /**
     * @param $id
     * @return string
     * ajax请求，用来得到对应class1的class2的值
     */
    public function actionGetreadtype2($id)
    {
        $class1model = new Class2();
        $class1 = $class1model->find()->where(["class1_id" => $id])->all();
        $class1info = "";
        foreach ($class1 as $s) {
            $class1info .= $s->id . " " . $s->name . "/";
        }
        return $class1info;
    }
    

    /**
     * @return string
     * 添加文章控制器
     */
    public function actionReadadd()
    {
        $session = Yii::$app->session;
        $request = \Yii::$app->request;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        if ($request->isPost)
        {
            $type=$request->post('type');
            if($type==1)
            {
                $readinfo = array(
                    "title" => $request->post('title'),
                    "info" => $request->post('info'),
                    "class1" => $request->post('class1'),
                    "class2" => $request->post('class2'),
                    "diff" => $request->post('diff'),
                    "imgurl" => $request->post('img_url'),
                    "content" => $request->post('editorValue'),
                    "class_num"=>$request->post('class_num'),
                    "passage"=>$request->post('passage')
                );
                $readcontentmodel  = new ReadContent();
                $readcontentmodel->title = $readinfo['title'];
                $readcontentmodel->info = $readinfo['info'];
                $readcontentmodel->class1_id = $readinfo['class1'];
                $readcontentmodel->class2_id = $readinfo['class2'];
                $readcontentmodel->diff = $readinfo['diff'];
                $readcontentmodel->img_url = $readinfo['imgurl'];
                $readcontentmodel->class_num=$readinfo['class_num'];
                $readcontentmodel->passage=$readinfo['passage'];
                $session['new_article']=$readcontentmodel;//文章暂存
                return $this->render("article",["article" =>$readinfo['content']]);
            }elseif ($type==2)
            {
                if(!isset($session['new_article']))
                    die('session错误');
                $readcontentmodel=$session['new_article'];
                $readcontentmodel->content = $request->post('article');
                $readstatus = $readcontentmodel->save();
                if ($readstatus)
                {
                    $id = $readcontentmodel->attributes['id'];
                    $this->redirect(["readupdate","id" =>$id ]);
                }else
                {
                    echo "<script>alert('添加文章失败，请重试');
    			            location.href='?r=admin/readadd';
    		                </script>";
                    die;
                }
            }

        } else {
            return $this->render('readadd');
        }
    }

    /**
     * @param $id
     * @return string
     * 修改文章控制器
     */
    public  function actionContentupdate($id)
    {
        $session = Yii::$app->session;
        $request = \Yii::$app->request;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        if ($request->isPost)
        {
            $type=$request->post('type');
            if($type==1)
            {
                $readinfo = array(
                    "title" => $request->post('title'),
                    "info" => $request->post('info'),
                    "class1" => $request->post('class1'),
                    "class2" => $request->post('class2'),
                    "diff" => $request->post('diff'),
                    "imgurl" => $request->post('img_url'),
                    "content" => $request->post('editorValue'),
                    "class_num"=>$request->post('class_num'),
                    "passage"=>$request->post('passage')
                );
//                $readcontentmodel  = new ReadContent();
                $read =ReadContent::findOne($id);
                $read->title = $readinfo['title'];
                $read->info = $readinfo['info'];
                $read->class1_id = $readinfo['class1'];
                $read->class2_id = $readinfo['class2'];
                $read->diff = $readinfo['diff'];
                $read->img_url = $readinfo['imgurl'];
                $read->class_num=$readinfo['class_num'];
                $read->passage=$readinfo['passage'];
                $session['new_article']=$read;//文章暂存
                return $this->render("article1",["article" =>$readinfo['content'],"id" => $id]);
            }elseif ($type==2)
            {
                if(!isset($session['new_article']))
                    die('session错误');
                $read=$session['new_article'];
                $read->content = $request->post('article');
                $readstatus = $read->update();
                if ($readstatus)
                {
                    $id = $read->attributes['id'];
                    $this->redirect(["readupdate","id" =>$id ]);
                }else
                {
                    header('Content-Type:text/html;charset=utf-8');
                    echo "<script>alert('修改文章失败，请重试');
    			            location.href='?r=admin/readadd';
    		                </script>";
                    die;
                }
            }

        } else {
            $model = new ReadContent();
            $info = $model->find()->where(["id" => $id])->one();
            return $this->render('contentupdate',["info" => $info]);
        }
    }
    public function actionContentupdate1($id)
    {
        $session = Yii::$app->session;
        $request = \Yii::$app->request;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        if ($request->isPost){
            $readinfo = array(
                "title" => $request->post('title'),
                "info" => $request->post('info'),
                "class1" => $request->post('class1'),
                "class2" => $request->post('class2'),
                "diff" => $request->post('diff'),
                "imgurl" => $request->post('img_url'),
                "content" => $request->post('editorValue'),
                "class_num"=>$request->post('class_num'),
                "passage"=>$request->post('passage')
            );
            $read =ReadContent::findOne($id);
            $read->title = $readinfo['title'];
            $read->info = $readinfo['info'];
            $read->class1_id = $readinfo['class1'];
            $read->class2_id = $readinfo['class2'];
            $read->diff = $readinfo['diff'];
            $read->img_url = $readinfo['imgurl'];
            $read->class_num=$readinfo['class_num'];
            $read->passage=$readinfo['passage'];
            //$read->content = $readinfo['content'];
            $readstatus = $read->update();
            $this->redirect(["readupdate","id" =>$id ]);
        }
    }

    /**
     * @param $id
     * @return mixed
     * 根据id查询到class1 的值
     */
    public function actionGetclass1($id)
    {
        $model = new Class1();
        $name = $model->find()->where(["id" => $id])->one()->name;
        return $name;
    }

    /**
     * @param $id
     * @return mixed
     * 根据id查询到class2的值
     */
    public function actionGetclass2($id)
    {
        $model = new Class2();
        $name = $model->find()->where(["id" => $id])->one()->name;
        return $name;
    }


    /**
     * @param int $id
     * @return string
     * 阅读文章以及小题选项查询控制器
     */
    public function actionReadupdate($id =0)
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $readmodel1 = new ReadContent();
        $readmodel2 = new ReadQuestion();
        $readinfo1 = $readmodel1->find()->where(["id" => $id])->one();
        $readinfo2 = $readmodel2->find()->where(['read_content_id' => $readinfo1->id])->all();
        if ($readinfo1->diff == 1) {
            $diff = "简单";
        } elseif ($readinfo1->diff == 2) {
            $diff = "适中";
        } else {
            $diff = "难";
        }
        $class1model = new Class1();
        $class1 = $class1model->find()->where(["id" => $readinfo1->class1_id])->one();
        $class1mode2 = new Class2();
        $class2 = $class1mode2->find()->where(["id" => $readinfo1->class2_id])->one();
        $readinfo = array(
            "id" => $readinfo1->id,
            "title" => $readinfo1->title,
            "info" => $readinfo1->info,
            "content" => $readinfo1->content,
            "tnum" => count($readinfo2),
            "num" => $readinfo1->num,
            "diff" => $diff,
            "class1" => $class1->name,
            "class2" => $class2->name,
            "imgurl" => $readinfo1->img_url
        );
        $optionmodel = new ReadOption();
        foreach ($readinfo2 as $s){
            $optioninfo = $optionmodel -> find()->where(["read_question_id" => $s->id])->all();
            $readinfo['question'][$s->id]=array(
                "qid" => $s->id,
                "tnumm" => $s->tnum,
                "content" => $s->content,
                "answer" => $s->answer,
                "type" => $s->type,
                "t_type" => $s->t_type,
                "paragraph" => $s->paragraph,
                "options" =>array(),
            );
            foreach ($optioninfo as $v) {
                $readinfo['question'][$s->id]['options'][] = array(
                    "id" => $v->id,
                    "option" => $v->options,
                    "content" => $v->content,
                );
            }
        }
        return $this-> render('readupdate' ,array("readinfo" => $readinfo));
    }


    /**
     * @param $id
     * @return string
     * 添加小题控制器
     */
    public function actionReadquestion($id)
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $request = \Yii::$app->request;
        if ($request->isPost){
            $tnum = $request->post('tnum');
            $content = $request->post('content');
            $type = $request->post('type');
            $paragraph = $request->post('paragraph');
            $t_type = $request ->post('t_type');
            $readquestionmodel = new ReadQuestion();
            $readquestionmodel->read_content_id = $id;
            $readquestionmodel->tnum = $tnum;
            $readquestionmodel->content = $content;
            $readquestionmodel->paragraph = $paragraph;
            $readquestionmodel->t_type = $t_type;
            $answer = "";
            if ($t_type=='10'){
                $table_a = strtolower($request->post('table_a'));
                $table_b = strtolower($request->post('table_b'));
                $answera = explode(" ",$table_a);
                $answerb = explode(" ",$table_b);
                $a = 3;
                $b = 3;
                $c = 3;
                $d = 3;
                $e = 3;
                $f = 3;
                $g = 3;
                if (in_array('a',$answera)){
                    $a = 1;
                }
                if (in_array('a',$answerb)){
                    $a = 2;
                }
                if (in_array('b',$answera)){
                    $b = 1;
                }
                if (in_array('b',$answerb)){
                    $b = 2;
                }
                if (in_array('c',$answera)){
                    $c = 1;
                }
                if (in_array('c',$answerb)){
                    $c = 2;
                }
                if (in_array('d',$answera)){
                    $d = 1;
                }
                if (in_array('d',$answerb)){
                    $d = 2;
                }
                if (in_array('e',$answera)){
                    $e = 1;
                }
                if (in_array('e',$answerb)){
                    $e = 2;
                }
                if (in_array('f',$answera)){
                    $f = 1;
                }
                if (in_array('f',$answerb)){
                    $f = 2;
                }
                if (in_array('g',$answera)){
                    $g = 1;
                }
                if (in_array('g',$answerb)){
                    $g = 2;
                }
                $answer = "[".$a.",".$b.",".$c.",".$d.",".$e.",".$f.",".$g."]";
            }else {
                foreach ($request->post() as $s => $v) {
                    if (strpos($s, "correct")) {
                        $answer .= chr(substr($s, 0, 1) + 64);
                    }
                }
            }
                $readquestionmodel->answer = $answer;
                $readquestionmodel->type = $type;
                $status = $readquestionmodel->save();
                foreach ($request->post() as $m => $n) {
                    if (is_int($m)) {
                        $options = chr(substr($m, 0, 1) + 64);
                        $content = $request->post("$m");
                        if ($request->post($m . '_correct')) {
                            $correct = 1;
                        } else {
                            $correct = 0;
                        }
                        $optionmodel = new ReadOption();
                        $optionmodel->read_question_id = $readquestionmodel->id;
                        $optionmodel->options = $options;
                        $optionmodel->content = $content;
                        $optionmodel->is_correct = $correct;
                        $optionmodel->save();
                    }
                }
           if ($status){
               echo "<script>alert('保存成功');
   			location.href='?r=admin/readupdate&id=".$id."';
   		</script>";
               die;
           }
       }else {
           return $this->render('readquestion', ["id" => $id]);
        }
    }

    /**
     * @param $id
     * @param $qid
     * @return string
     * 小题修改控制器
     */
    public function actionReadquestionupdate($id,$qid)
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $request = \Yii::$app->request;
        if ($request->isPost){
            $tnum = $request->post('tnum');
            $content = $request->post('content');
            $type = $request->post('type');
            $t_type = $request->post('t_type');
            $readquestionmodel = new ReadQuestion();
            $read = $readquestionmodel->findOne($qid);
            $read -> read_content_id = $id;
            $read -> tnum = $tnum;
            $read -> content = $content;
            $read -> t_type = $t_type;
            $answer = "";
            foreach ($request->post() as $s => $v){
                if (strpos($s,"correct")){
                    $answer.=chr(substr($s ,0 ,1)+64);
                }
            }
            $read->answer = $answer;
            $read->type = $type;
            $status = $read->update();
            foreach ($request->post() as $m => $n){
                if(is_int($m)){
                    $options = chr(substr($m ,0 ,1)+64);
                    $content = $request->post("$m");
                    if($request->post($m.'_correct')){
                        $correct = 1;
                    }else{
                        $correct = 0;
                    }
                    $id1 = $request->post($m."_id");
                    $optionmodel = new ReadOption();
                    $option = $optionmodel->findOne($id1);
                    $option->read_question_id = $qid;
                    $option->options = $options;
                    $option->content = $content;
                    $option->is_correct = $correct;
                    $status1["$m"] = $option->update();
                }
            }
            if ($status||$status1['1']||$status1['2']||$status1['3']||$status1['4']){
                echo "<script>alert('修改小题成功！');
    			location.href='?r=admin/readupdate&id=".$id."';
    		</script>";
                die;
            }else{
                echo "<script>alert('修改小题失败，请重试');
    			location.href='?r=admin/readupdate&id=".$id."';
    		</script>";
                die;
            }
        }else {
            $info = ReadQuestion::findOne($qid);
            $option = ReadOption::find()->where(["read_question_id" => $qid])->all();
            return $this->render('readquestionupdate', ["id" => $id,"qid" => $qid,"info" => $info,"option" => $option]);
        }
    }

    /**
     * @param $id
     * @param $qid
     * 小题删除控制器
     */
    public function actionReadquestiondelete($id,$qid)
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $status = ReadQuestion::findOne($qid);
        $status = $status->delete();
        if($status){
            echo "<script>alert('小题删除成功');
    			location.href='?r=admin/readupdate&id=".$id."';
    		</script>";
            die;
        }else{
            echo "<script>alert('小题删除失败请重试');
    			location.href='?r=admin/readupdate&id=".$id."';
    		</script>";
            die;
        }
    }


    /**
     * @return string
     * class1查询控制器
     */
    public function actionClass1manage()
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $class1info = Class1::find()->all();
        return $this->render('class1manage',["class1info" => $class1info]);
    }

    /**
     * @param $id
     * class1删除控制器
     */
    public function actionClass1del($id)
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $info = Class2::find()->where(["class1_id" => $id])->all();
        if($info){
            echo "<script>alert('此分类下存在二级结构,请删除以后在删除！');
    			location.href='?r=admin/class1manage';
    		</script>";
            die;
        }else {
            $class1model = new Class1();
            $delstatus = $class1model->find()->where(["id" => $id])->one();
            $delstatus = $delstatus->delete();
            if ($delstatus) {
                echo "<script>alert('删除成功');
    			location.href='?r=admin/class1manage';
    		</script>";
                die;
            } else {
                echo "<script>alert('删除失败，请重试！');
    			location.href='?r=admin/class1manage';
    		</script>";
                die;
            }
        }
    }

    /**
     * @param $id
     * @return string
     * class1更新控制器
     */
    public function actionClass1update($id)
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $request = \Yii::$app->request;
        if($request->isPost){
            $name = $request->post('name');
            $class1 = Class1::findOne($id);
            $class1->name = $name;
             $status = $class1->update();
            if($status){
                echo "<script>alert('修改成功');
    			location.href='?r=admin/class1manage';
    		</script>";
                die;
            } else {
                echo "<script>alert('修改失败，请重试！');
    			location.href='?r=admin/class1manage';
    		</script>";
                die;
            }
        }else {
            $class1info = Class1::find()->where(["id" => $id])->one();
            return $this->render('class1update', ["class1info" => $class1info]);
        }
    }

    /**
     * @return string
     * class2查询控制器
     */
    public function actionClass2manage()
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $class2info = Class2::find()->all();
        foreach ($class2info as $s){
            $s->class1_id = Class1::findOne($s->class1_id)->name;
        }
        return $this->render('class2manage',["class2info" => $class2info]);
    }

    /**
     * @param $id
     * class2删除控制器
     */
    public function actionClass2del($id)
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $info = ReadContent::find()->where(["class2_id" => $id])->all();
        $indo1 = SpeakContent::find()->where(["class2_id"=> $id])->all();
        $info2 = WriteQuestion::find()->where(["class2_id"=> $id])->all();
        if($info||$indo1||$info2){
            echo "<script>alert('此分类下存在题目,请删除以后在删除！');
    			location.href='?r=admin/class1manage';
    		</script>";
            die;
        }else {
            $class2model = new Class2();
            $delstatus = $class2model->find()->where(["id" => $id])->one();
            $delstatus = $delstatus->delete();
            if ($delstatus) {
                echo "<script>alert('删除成功');
    			location.href='?r=admin/class2manage';
    		</script>";
                die;
            } else {
                echo "<script>alert('删除失败，请重试！');
    			location.href='?r=admin/class2manage';
    		</script>";
                die;
            }
        }
    }

    /**
     * @param $id
     * @return string
     * class2更新控制器
     */
    public function actionClass2update($id)
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $request = \Yii::$app->request;
        if($request->isPost){
            $name = $request->post('name');
            $class1_id = $request->post('class1');
            $class1 = Class2::findOne($id);
            $class1->name = $name;
            $class1->class1_id=$class1_id;
            $status = $class1->update();
            if($status){
                echo "<script>alert('修改成功');
    			location.href='?r=admin/class2manage';
    		</script>";
                die;
            } else {
                echo "<script>alert('修改失败，请重试!');
    			location.href='?r=admin/class2manage';
    		</script>";
                die;
            }
        }else {
            $class1info = Class2::find()->where(["id" => $id])->one();
            return $this->render('class2update', ["class1info" => $class1info]);
        }
    }

    /**
     * @return string
     * class2添加控制器
     */
    public function actionClass2add()
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $request = \Yii::$app->request;
        if ($request->isPost){
            $name = $request->post('name');
            $class1_id = $request->post('class1');
            $model = new Class2();
            $model->name = $name;
            $model->class1_id = $class1_id;
            $status = $model->save();
            if ($status){
                echo "<script>alert('添加成功');
    			location.href='?r=admin/class2manage';
    		</script>";
                die;
            } else {
                echo "<script>alert('添加失败，请重试!');
    			location.href='?r=admin/class2manage';
    		</script>";
                die;
            }
        }else {
            return $this->render('class2add');
        }
    }

    public function actionMember()
    {
        $session = Yii::$app->session;
        if (!$session['is_admin']) {
            echo "<script>alert('请登录');
    			location.href='?r=admin/login';
    		</script>";
            die;
        }
        $member = User::find()->all();
        return $this->render('member',array("member"=>$member));
    }
    public function actionGetimgurl($id)
    {
        $operate = ReadContent::findOne($id);
        $res = $operate->img_url;
        echo json_encode($res);
    }
}